module MenuSystem
  
  class MenuItem
    attr_accessor :url, :name
    
    def initialize(url, name)
      self.url = url
      self.name = name
    end
  end
  
  class Menu
    attr_accessor :current_item, :items
    
    def initialize
      self.items = Array.new
    end
    
    def add_menu_item(url, name)
      self.items << MenuItem.new(url, name)
    end
    
    def itemid_by_url(url)
      self.items.each_index do |key|
        menu_item = self.items[key]
        if menu_item.url == url
          return key.to_i
        end
      end
      
      false
    end
    
  end

  def init_menu
    if !@menu
      @menu = MenuSystem::Menu.new
    end
  end
  
  def show_menu
    
  end
  
  def current_item
    if @menu.itemid_by_url(request.request_uri)
      session[:current_item] = @menu.itemid_by_url(request.request_uri)
    end
    
    @menu.items[session[:current_item]]
  end
  
  def add_menu_item(url, name)
    init_menu
    @menu.add_menu_item(url, name)
  end
  
    def self.included(base)
      base.send :helper_method, :current_item, :add_menu_item
    end
  
end